#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    doctorCommunicator = new DoctorCommunicator("192.168.233.1", 12345, this);

    // 设置界面布局
    QWidget *centralWidget = new QWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(centralWidget);

    // 请求输入框
    requestInput = new QLineEdit(centralWidget);
    requestInput->setPlaceholderText("输入请求内容...");
    layout->addWidget(requestInput);

    // 响应显示区域
    responseDisplay = new QTextEdit(centralWidget);
    responseDisplay->setReadOnly(true);
    layout->addWidget(responseDisplay);

    // 医生请求按钮
    QPushButton *doctorButton = new QPushButton("发送医生请求", centralWidget);
    layout->addWidget(doctorButton);
    connect(doctorButton, &QPushButton::clicked, this, &MainWindow::onDoctorRequest);

    setCentralWidget(centralWidget);

    // 连接患者端和医生端的响应信号到显示槽
    connect(doctorCommunicator, &DoctorCommunicator::responseReceived, this, &MainWindow::displayDoctorResponse);
}

void MainWindow::onDoctorRequest()
{
    QString request = "DOCTOR_REQUEST:" + requestInput->text();  // 发送的请求内容
    doctorCommunicator->sendRequest(request);  // 发送医生请求
}

void MainWindow::displayDoctorResponse(const QString &response)
{
    responseDisplay->append(response);  // 显示医生端的响应
}

MainWindow::~MainWindow()
{
    delete ui;
}
